<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        html,
        body {
            height: 100%;
        }
        
        #app {
            height: 100%;
            position: relative;
            background-color: rgba(0, 0, 0, .5);
        }
        
        .shopcart {
            position: fixed;
            bottom: 0;
            background-color: white;
            width: 100%;
            padding: 10px;
            box-sizing: border-box;
        }
        
        .shopcart .title {
            display: flex;
            justify-content: space-between;
            font-weight: bold;
        }
        
        .shopcart .list {
            margin-bottom: 60px;
        }
        
        .shopcart .list .good {
            display: flex;
            margin: 10px 0;
        }
        
        .shopcart .list .good .imgbox {
            width: 80px;
            margin-right: 10px;
        }
        
        .shopcart .list .good .imgbox img {
            width: 100%;
        }
        
        .shopcart .list .good .text {
            flex: 1;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }
        
        .shopcart .list .good .text .name {
            font-weight: bold;
        }
        
        .shopcart .list .good .text .price {
            display: flex;
            justify-content: space-between;
        }
        
        .shopcart .list .good .text .price span {
            display: inline-block;
            width: 14px;
            height: 14px;
            text-align: center;
            line-height: 14px;
            background-color: lightskyblue;
            color: white;
            border-radius: 50%;
        }
        
        .shopcart .list .good .text .price span:nth-child(2) {
            color: black;
            background-color: white;
        }
        
        .empty {
            text-align: center;
            padding: 50px;
        }
        
        .summary {
            display: flex;
            justify-content: space-between;
            align-items: center;
            background-color: #555;
            color: white;
            left: 0;
            position: fixed;
            bottom: 0;
            width: 100%;
            padding: 10px;
            box-sizing: border-box;
        }
        
        .summary span:nth-child(2) {
            background-color: lightgreen;
            color: white;
            padding: 5px 15px;
            border-radius: 5px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="shopcart">
            <div class="title">
                <span>已选商品</span>
                <span @click="clean">清空</span>
            </div>

            <!-- v-if , v-else 条件渲染指令 用来控制标签的显示/隐藏, 他俩必须放置在两个兄弟标签上, 条件成立时渲染该标签, 条件不成立不渲染标签-->
            <!-- v-if 和 v-for 放在同一个标签上时, v-for的优先级更高, v-if就会失效, 需要避免这样的用法 -->
            <div v-if="list.length != 0" class="list">
                <div v-for="(item,index) in list" :key="index" class="good">
                    <div class="imgbox">
                        <img :src="item.pic" alt="">
                    </div>
                    <div class="text">
                        <div class="name">{{item.name}}</div>
                        <!-- v-html , v-text 都可以绑定数据到标签内容 和 {{}} 的作用一样 -->
                        <!-- v-html 可以解析标签 -->
                        <!-- v-text 无法解析标签 -->
                        <!-- <div class="name" v-html="item.name"></div> -->
                        <!-- <div class="name" v-text="item.name"></div> -->
                        <div class="price">
                            <div>¥{{item.price}}</div>
                            <div>
                                <span @click="sub(item)">-</span>
                                <span>{{item.count}}</span>
                                <span @click="add(item)">+</span>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- v-show 通过条件控制标签的显示隐藏, 本质上是通过控制标签的css样式display 实现标签的显示/隐藏 -->
                <!-- v-show 无论条件是否成立, 标签肯定会渲染 -->
                <!-- <div class="empty" v-show="list.length == 0">购物车空空如也,去逛逛!</div> -->
            </div>
            <div v-else class="empty">购物车空空如也,去逛逛!</div>

            <div class="summary">
                <span>总数:{{totalCount}} 总价:{{totalPrice}} </span>
                <span>去结算</span>
            </div>
        </div>
    </div>

</body>

</html>
<script src="../vue-2.5.21.js"></script>
<script>
    var pic = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202102%2F26%2F20210226073347_50f94.thumb.1000_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1668131440&t=6df291a148cdb21289e91211d4daea32';
    new Vue({
        el: "#app",
        data: {
            list: [{
                name: '秋刀鱼',
                price: 88,
                pic,
                count: 1
            }, {
                name: '大黄鱼',
                price: 777,
                pic,
                count: 2
            }, {
                name: '皮皮虾',
                price: 9,
                pic,
                count: 3
            }, ]
        },
        methods: {
            add(item) {
                item.count++;
            },
            sub(item) {
                item.count--;
            },
            clean() {
                this.list = [];
            }
        },
        computed: { //负责定义计算属性
            //1.计算属性在定义时像函数,使用时像只读变量
            //2.当计算属性内部所依赖的数据发生改变时, 计算属性内部的代码会自动重新执行,计算出一个新值, 这个新值作为计算属性的值.
            //3.如果计算属性内部所依赖的数据未发生改变, 则计算属性不会重新计算, 而是使用第一次计算的缓存结果作为下一次使用的值( 计算属性会缓存计算的结果 ).
            //写法一：回调函数数
            totalCount() {
                console.log('计算属性: totalCount 执行了');
                var sum = 0;
                this.list.forEach(item => {
                    sum += item.count;
                });
                return sum;
            },
            // 写法二：setter&getter
            totalPrice: {
                get() { //get方法会在使用（渲染）计算属性时自动执行
                    var sum = 0;
                    this.list.forEach((item) => {
                        sum += item.count * item.price;
                    })
                    return sum;
                }
            },
            set() { //set方法会在 给计算属性设置值时执行
                console.log('给totalPrice设置值');
            }
        }
    })
</script>